package com.lcz.googleplay.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.lcz.googleplay.R;
import com.lcz.googleplay.domain.HomeInfo;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.utils.ViewUtils;
import com.lcz.googleplay.widget.LoadingFrameLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by hp on 2016/10/26.
 */

public abstract class BaseFragment extends Fragment {
    private LoadingFrameLayout frameLayout;//替换成自定义的FrameLayout

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (frameLayout == null) {
            frameLayout = new LoadingFrameLayout(getActivity()) {
                @Override
                public View createSuccessView() {
                    return BaseFragment.this.createSuccessView();
                }

                @Override
                public LoadResult load() {
                    return BaseFragment.this.load();
                }
            };

        } else {
            ViewUtils.removeParent(frameLayout);
        }

        return frameLayout;
    }

    public void show() {
        if (frameLayout != null) {
            frameLayout.show();
        }
    }

    public LoadingFrameLayout.LoadResult checkData(List list) {
        if (list == null) {
            return LoadingFrameLayout.LoadResult.error;
        } else {
            if (list.size() == 0) {
                return LoadingFrameLayout.LoadResult.empty;
            } else {
                return LoadingFrameLayout.LoadResult.success;
            }
        }
    }

    /**
     * 成功界面是所有fragment中不同的界面
     * 返回成功的界面
     *
     * @return
     */
    public abstract View createSuccessView();

    /**
     * 请求服务器
     */
    public abstract LoadingFrameLayout.LoadResult load();
}
